package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 删除套餐及套餐菜品表
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //构建查询构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加查询条件
        lqw.eq(Setmeal::getStatus,1);
        lqw.in(Setmeal::getId,ids);
        //执行查询
        int count = count(lqw);
        //判断删除套餐内有无已启用套餐
        if (count > 0){
            throw new CustomException("套餐处于起售状态，无法删除");
        }
        //删除套餐表
        removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.in(SetmealDish::getSetmealId,ids);
        //删除套餐菜品表
        setmealDishService.remove(lqw1);
    }

    /**
     * 新增菜品
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveSetmeaWithDish(SetmealDto setmealDto) {
        //添加到套餐表
        save(setmealDto);
        //获取前端传来的套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
       //获取套餐id
        Long id = setmealDto.getId();
        //给新增的菜品添加id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        //存储进套餐菜品表
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id查询套餐信息及套餐内菜品
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDto getByIdWithDish(Long id) {
        //根据id获取套餐信息
        Setmeal setmeal = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //拷贝数组
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //添加条件查询
        lqw.eq(SetmealDish::getSetmealId,id);
        //执行查询，查询该套餐下所有菜品
        List<SetmealDish> dishList = setmealDishService.list(lqw);
        //存储进dto表
        setmealDto.setSetmealDishes(dishList);

        return setmealDto;
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateSetmealWithDish(SetmealDto setmealDto) {
        //更新套餐表信息
        updateById(setmealDto);
        //获取页面传回的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //获取id
        Long id = setmealDto.getId();
        //构造条件构造器
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //添加查询条件
        lqw.eq(SetmealDish::getDishId, id);
        //删除原有菜品信息
        setmealDishService.remove(lqw);
        //给菜品表id赋值
        setmealDishes.forEach(setmealDish ->
                setmealDish.setSetmealId(id));
        //存储新的菜品信息
        setmealDishService.saveBatch(setmealDishes);
    }
}
